<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('物流拼票界面')" />
	<th:block th:include="include :: businessCSS" />
	<style>
        .main-content{position: relative;}
        .topbtns{position:fixed;top: 0;width: 100%;z-index: 9999;background-color: #fff;}
		.preservationBtn{padding: 10px;margin: 0;position: fixed;top: 0;z-index: 9999;background: #fff;width: 100%;}
		.preservationBtn input{font-size: 16px;background-color: rgb(80, 138, 238);color: #fff;border: none;padding: 5px 30px;border-radius: 5px;cursor: pointer;}
		.layui-table-tool-temp{padding-right: 30px;}
        .getDetailUpload,.getDetailUpload2{cursor: pointer;}
        .layui-table-tool-self{display: none;}
		.tablebody{background-color: #fff;padding: 20px;margin-right: 15px;}
		.tablebody input{display: inline;width: 80%;border: none;}
		.tablebody table{width: 100%;}
		.tablebody th,.tablebody td{border:solid 1px #ddd;padding:10px;font-size:14px;text-align: center;}
	</style>
</head>
<body class="no-skin">
<div class="main-container" id="main-container">
	<div class="main-content">
	<form class="layui-form head-form">
<!--        <div class="topbtns"><th:block th:include="include :: business_btn" /></div>-->
	    <div class="north" style="background-color: #fff;margin-top: 55px;">
			 <div class="layui-form-item preservationBtn">
                <button class="layui-btn layui-btn-normal dhicon btn-edit" type="button" lay-submit lay-filter="btn-edit" id="btn-edit">生成物流订单</button>
			</div>
<!--			<div style="width: 100%;height: 65px;background: #f6f6f6;"></div>-->
			<div class="forminput"></div>
    	</div>
        <div style="width: 100%;height: 10px;background: #f6f6f6;"></div>
        <div>
            <table class="layui-hide" id="mjdhtable" lay-filter="mjtable"></table>
            <script type="text/html" id="zjtoolbar">
                <div style="padding: 16px;display: flex;justify-content: space-between;">
                    <div  style="font-weight: 700;font-size: 18px;">拼票预测</div>
            	</div>
            </script>
        </div>
        <th:block th:include="include :: pageFooter" />
    </form>
	</div>
</div>

<th:block th:include="include :: footer" />
<th:block th:include="include :: businessJS" />
<th:block th:include="include :: inlineJS" />


<script th:inline="javascript">
	/*<![CDATA[*/
	var fields = [[${fields}]];
	var fieldsList = [[${fieldsList}]];
	var fieldsHiddenList = [[${fieldsHiddenList}]];
	var gridColumns= [[${gridColumns}]];
	var stringObjectMap= [[${stringObjectMap}]];
	var type= [[${type}]];
	var guidList= [[${guidList}]];
	var all_fields= fieldsList;
	var $,layer,form,table;
	var enableEdit = [[${enableEdit}]];
	var listData1 = [];
	var guidLogsticsPattern;
	var numMaxProName;
    var tablecols=[ [
		,{align: 'center',field: 'shipmentId', title: '订单', width: 130}
		,{align: 'center',field: 'actualLogisticsFreight', title: '物流总费用', width: 100}
		,{align: 'center',field: 'numBox', title: '箱数', width: 90}
		,{align: 'center',field: 'weightReal', title: '实重', width: 90}
		,{align: 'center',field: 'weightVol', title: '抛重', width: 90}
		,{align: 'center',field: 'weightCal', title: '计重', width: 90}
		,{align: 'center',field: 'priceTranCalWeightPredict', title: '计重费', width: 100}
        ,{align: 'center',field: 'surchargeMaterialMax', title: '附加材质', width: 100}
        ,{align: 'center',field: 'priceQualityExtraPredict', title: '材质附加费', width: 100}
        ,{align: 'center',field: 'numProName', title: '品名数', width: 100}
        ,{align: 'center',field: 'priceProNameExtraPredict', title: '品名附加费', width: 100}
        ,{align: 'center',field: 'priceClearanceOpe', title: '清关费', width: 100}
        ,{align: 'center',field: 'jettisonCoe', title: '抛货系数', width: 100}
        ,{align: 'center',field: 'aveBoxWeight', title: '平均箱重', width: 100}
    ]]


	var $,layer,form,table,nid,noTable;
    layui.use(['laydate','form','layer','jquery','util','element','upload','table'], function () {
		$ = layui.$, layer = layui.layer, form = layui.form, laydate = layui.laydate, util = layui.util, element = layui.element, upload = layui.upload, table = layui.table;
		//初始化操作
		headcom3()
		nid = $('#nid').val()
		// 保存按钮事件变化
		$(function () {
			$('.tp-save').attr('onclick', '')
			$('.tp-save').attr('lay-filter', 'btn-edit')
			$('.tp-save').attr('lay-submit', true)
		})

		// 保存操作
		form.on('submit(btn-edit)', function(data){
			var is_return = false//是否中断保存
			fieldsList.map((v,i)=>{
				if(v.fieldFrontRequired){
					if(!$('#'+v.fields).val()){
						layer.msg(v.fieldName+'不能为空')
						is_return=true
					}
				}
			})
			if(is_return) return;
			if (listData1.length == 0){
				layer.msg("拼票预测为空,请核实后在操作!");
				is_return=true;
			}
			if(is_return) return;
			if (type == 1) {
				listData1.map((v, i) => {
					if(is_return) return;
					if (v.shipmentId == '差额金额') {
						if (Number(v.actualLogisticsFreight) < 0) {
							$.modal.confirmCom('差额为负数,确定继续生成物流订单？', function (result) {
								$.modal.loading("正在处理中，请稍后...");
								$.post("/logstics/fbaSpellTicket/editpost", {
									type: type,
									guidList: guidList,
									infoItems: JSON.stringify(listData1),
									guidLogsticsPattern:guidLogsticsPattern
								}, function (res) {
									$.modal.closeLoading()
									layer.msg(res.msg);
								});
							});
						}else {
							$.modal.loading("正在处理中，请稍后...");
							$.post("/logstics/fbaSpellTicket/editpost", {
								type: type,
								guidList: guidList,
								infoItems: JSON.stringify(listData1),
								guidLogsticsPattern:guidLogsticsPattern
							}, function (res) {
								$.modal.closeLoading()
								layer.msg(res.msg);
							});
						}
					}else if (v.shipmentId == '拼票金额') {
					    if (Number(v.numProName) > Number(numMaxProName)) {
							layer.msg("拼票的品名数超过物流名称中的品名上限,不可拼票");
							is_return=true;
						}
					}
				})
			} else {
				$.post("/logstics/fbaSpellTicket/editpost", {
					type: type,
					guidList:guidList,
					infoItems: JSON.stringify(listData1),
					guidLogsticsPattern:guidLogsticsPattern
				}, function (res) {
					$.modal.closeLoading()
					layer.msg(res.msg);
				});
			}
		});

		let cols;
		if (type == 1){
			cols = tablecols;
		} else {
			cols = [stringObjectMap.gridBody];
		}
		// 初始化表格
		$.tablelist.init({
			elem: '#mjdhtable'
			, data: listData1
			, page: false
			, even: true
			, limit: 10000
			, totalRow: false
			, cols: cols
			, toolbar: '#zjtoolbar'
			, defaultToolbar: [{title: '列设置', layEvent: 'fieldSet', icon: 'layui-icon-set'}]
			, height: '550'
			, done: function (res, curr, count) {
				listData1 = table.cache.mjdhtable;//每次重载都赋值更新表缓存

				//更新装箱清单样式
				var that = this.elem.next();
				res.data.forEach(function (item, index) {
					if (item.shipmentId.indexOf("FBA") == -1) {
						var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("background-color", '#e5f3fe');
					} else {
						var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("background-color", 'white');
					}
				})
			}, success: function (a, b, c) {
				console.log(a, b, c)
			}
		});
	})

	/**
	 * 回调弹框事件
	 */
	function getMoreAfter(fieldsList,fields,tindex,fieldName,fieldSearchType,isSearch,isLayer,res) {//弹出框赋值回调
		if(fields=='nameLogsticsPattern'){
			//根据物流方式guid获取详情信息
			guidLogsticsPattern = res.guid;
			numMaxProName = res.numMaxProName;
			$.get('/logstics/fbaSpellTicket/details',{type:type,guidList:guidList,guidLogsticsPattern:guidLogsticsPattern},function(ress){
				if(ress.count != 0){
					listData1 = ress.data;
				} else {
					listData1 = []
				}
				table.reload('mjdhtable', {data:listData1});
			})
		}
	}

</script>

</body>
</html>